<template>
  <div class="home-header-container">
    <span class="title" @click="goHome" style="cursor: pointer">{{ title }}</span>
    <!-- <el-button style="background-color=#b598a1" round @click="exitUser">退出登录</el-button> -->
    <div class="user">
      <el-dropdown @command="handleCommand">
        <div class="profile">
          <img v-if="this.imgUrl === ' '" class="photo" src="@/assets/normalPeople.svg" alt="" />
          <img v-else class="photo" :src="this.imgUrl" @error="dufImg" alt="" />
          <div class="info">
            <span class="userName">{{ userName }}</span>
            <span class="departmentName">{{ departmentName }}</span>
          </div>
        </div>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="goHome" icon="el-icon-house">首页</el-dropdown-item>
          <el-dropdown-item command="goUserInfo" icon="el-icon-user">个人信息</el-dropdown-item>
          <el-dropdown-item command="logOut" icon="el-icon-turn-off">退出登录</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
export default {
  name: 'home-header',
  props: {
    imgUrl: {
      type: String /* 类型 */,
      default: '' /* 默认值 */,
      required: false /*必填  */,
    },
    logOut: {
      type: Function,
      default: () => {},
    },
    userName: {
      type: String,
      default: '默认用户',
    },
    departmentName: {
      type: String,
      default: '暂未分配',
    },
  },
  data() {
    return {
      title: '人力资源管理系统',
    }
  },
  methods: {
    // 返回首页
    goHome() {
      this.$router.push('/')
    },
    handleCommand(command) {
      // 调用返回首页
      if (command == 'goHome') {
        this.goHome()
      }
      // 调用父组件传递的函数
      else if (command == 'logOut') {
        this.logOut()
      } else if (command == 'goUserInfo') {
        this.$router.push('/userInfo')
      }
    },
    // 如果获取不到网络 图片 使用默认图片
    dufImg(e) {
      const img = e.srcElement
      ;(img.src = require('@/assets/normalPeople.svg')), (img.onerror = null) //防止闪图
    },
  },
}
</script>

<style lang="less" scoped>
.home-header-container {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-content: center;
  text-align: center;

  .title {
    color: #000;
    // margin-left: 70px;
    font-size: 20px;
  }
  .user {
    display: flex;
    flex-direction: column;
    justify-content: center;
    width: 200px;
    height: 60px;

    .profile {
      display: flex;
      flex-direction: row;
      align-content: center;
      height: 45px;
      width: 100%;
      box-shadow: 1px 1px 15px 1px #ccc;
      border-radius: 5px;
      padding: 5px;
      .photo {
        background-color: #000;
        height: 80%;
        border-radius: 10px;
        margin: 5px;
        cursor: pointer;
      }
      .info {
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        align-items: baseline;
        height: 80%;
        margin: 5px;
        line-height: 1;

        .departmentName {
          font-size: 12px;
          color: var(--themeSideTextColor);
        }
      }
    }
  }
}
</style>
